home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tclParse.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  35KB  |  1,247 lines

  1. /* 
  2.  * tclParse.c --
  3.  *
  4.  *    This file contains a collection of procedures that are used
  5.  *    to parse Tcl commands or parts of commands (like quoted
  6.  *    strings or nested sub-commands).
  7.  *
  8.  * Copyright (c) 1987-1993 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose, provided that the
  14.  * above copyright notice and the following two paragraphs appear in
  15.  * all copies of this software.
  16.  * 
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  *
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclParse.c,v 1.33 93/07/08 10:12:38 ouster Exp $ SPRITE (Berkeley)";
  31. #endif
  32.  
  33. #include "tclInt.h"
  34.  
  35. /*
  36.  * The following table assigns a type to each character.  Only types
  37.  * meaningful to Tcl parsing are represented here.  The table indexes
  38.  * all 256 characters, with the negative ones first, then the positive
  39.  * ones.
  40.  */
  41.  
  42. char tclTypeTable[] = {
  43.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  44.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  45.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  46.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  47.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  48.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  49.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  50.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  51.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  52.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  53.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  54.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  55.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  56.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  57.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  58.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  59.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  60.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  61.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  62.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  63.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  64.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  65.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  66.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  67.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  68.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  69.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  70.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  71.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  72.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  73.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  74.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  75.     TCL_COMMAND_END,   TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  76.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  77.     TCL_NORMAL,        TCL_SPACE,         TCL_COMMAND_END,   TCL_SPACE,
  78.     TCL_SPACE,         TCL_SPACE,         TCL_NORMAL,        TCL_NORMAL,
  79.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  80.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  81.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  82.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  83.     TCL_SPACE,         TCL_NORMAL,        TCL_QUOTE,         TCL_NORMAL,
  84.     TCL_DOLLAR,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  85.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  86.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  87.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  88.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  89.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_COMMAND_END,
  90.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  91.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  92.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  93.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  94.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  95.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  96.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  97.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_OPEN_BRACKET,
  98.     TCL_BACKSLASH,     TCL_COMMAND_END,   TCL_NORMAL,        TCL_NORMAL,
  99.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  100.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  101.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  102.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  103.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  104.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  105.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_OPEN_BRACE,
  106.     TCL_NORMAL,        TCL_CLOSE_BRACE,   TCL_NORMAL,        TCL_NORMAL,
  107. };
  108.  
  109. /*
  110.  * Function prototypes for procedures local to this file:
  111.  */
  112.  
  113. static char *    QuoteEnd _ANSI_ARGS_((char *string, int term));
  114. static char *    VarNameEnd _ANSI_ARGS_((char *string));
  115.  
  116. /*
  117.  *----------------------------------------------------------------------
  118.  *
  119.  * Tcl_Backslash --
  120.  *
  121.  *    Figure out how to handle a backslash sequence.
  122.  *
  123.  * Results:
  124.  *    The return value is the character that should be substituted
  125.  *    in place of the backslash sequence that starts at src.  If
  126.  *    readPtr isn't NULL then it is filled in with a count of the
  127.  *    number of characters in the backslash sequence.
  128.  *
  129.  * Side effects:
  130.  *    None.
  131.  *
  132.  *----------------------------------------------------------------------
  133.  */
  134.  
  135. char
  136. Tcl_Backslash(src, readPtr)
  137.     char *src;            /* Points to the backslash character of
  138.                  * a backslash sequence. */
  139.     int *readPtr;        /* Fill in with number of characters read
  140.                  * from src, unless NULL. */
  141. {
  142.     register char *p = src+1;
  143.     char result;
  144.     int count;
  145.  
  146.     count = 2;
  147.  
  148.     switch (*p) {
  149.     case 'a':
  150.         result = 0x7;    /* Don't say '\a' here, since some compilers */
  151.         break;        /* don't support it. */
  152.     case 'b':
  153.         result = '\b';
  154.         break;
  155.     case 'f':
  156.         result = '\f';
  157.         break;
  158.     case 'n':
  159.         result = '\n';
  160.         break;
  161.     case 'r':
  162.         result = '\r';
  163.         break;
  164.     case 't':
  165.         result = '\t';
  166.         break;
  167.     case 'v':
  168.         result = '\v';
  169.         break;
  170.     case 'x':
  171.         if (isxdigit(p[1])) {
  172.         char *end;
  173.  
  174.         result = strtoul(p+1, &end, 16);
  175.         count = end - src;
  176.         } else {
  177.         count = 2;
  178.         result = 'x';
  179.         }
  180.         break;
  181.     case '\n':
  182.         do {
  183.         p++;
  184.         } while (isspace(*p));
  185.         result = ' ';
  186.         count = p - src;
  187.         break;
  188.     case 0:
  189.         result = '\\';
  190.         count = 1;
  191.         break;
  192.     default:
  193.         if (isdigit(*p)) {
  194.         result = *p - '0';
  195.         p++;
  196.         if (!isdigit(*p)) {
  197.             break;
  198.         }
  199.         count = 3;
  200.         result = (result << 3) + (*p - '0');
  201.         p++;
  202.         if (!isdigit(*p)) {
  203.             break;
  204.         }
  205.         count = 4;
  206.         result = (result << 3) + (*p - '0');
  207.         break;
  208.         }
  209.         result = *p;
  210.         count = 2;
  211.         break;
  212.     }
  213.  
  214.     if (readPtr != NULL) {
  215.     *readPtr = count;
  216.     }
  217.     return result;
  218. }
  219.  
  220. /*
  221.  *--------------------------------------------------------------
  222.  *
  223.  * TclParseQuotes --
  224.  *
  225.  *    This procedure parses a double-quoted string such as a
  226.  *    quoted Tcl command argument or a quoted value in a Tcl
  227.  *    expression.  This procedure is also used to parse array
  228.  *    element names within parentheses, or anything else that
  229.  *    needs all the substitutions that happen in quotes.
  230.  *
  231.  * Results:
  232.  *    The return value is a standard Tcl result, which is
  233.  *    TCL_OK unless there was an error while parsing the
  234.  *    quoted string.  If an error occurs then interp->result
  235.  *    contains a standard error message.  *TermPtr is filled
  236.  *    in with the address of the character just after the
  237.  *    last one successfully processed;  this is usually the
  238.  *    character just after the matching close-quote.  The
  239.  *    fully-substituted contents of the quotes are stored in
  240.  *    standard fashion in *pvPtr, null-terminated with
  241.  *    pvPtr->next pointing to the terminating null character.
  242.  *
  243.  * Side effects:
  244.  *    The buffer space in pvPtr may be enlarged by calling its
  245.  *    expandProc.
  246.  *
  247.  *--------------------------------------------------------------
  248.  */
  249.  
  250. int
  251. TclParseQuotes(interp, string, termChar, flags, termPtr, pvPtr)
  252.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  253.                  * evaluations and error messages. */
  254.     char *string;        /* Character just after opening double-
  255.                  * quote. */
  256.     int termChar;        /* Character that terminates "quoted" string
  257.                  * (usually double-quote, but sometimes
  258.                  * right-paren or something else). */
  259.     int flags;            /* Flags to pass to nested Tcl_Eval calls. */
  260.     char **termPtr;        /* Store address of terminating character
  261.                  * here. */
  262.     ParseValue *pvPtr;        /* Information about where to place
  263.                  * fully-substituted result of parse. */
  264. {
  265.     register char *src, *dst, c;
  266.  
  267.     src = string;
  268.     dst = pvPtr->next;
  269.  
  270.     while (1) {
  271.     if (dst == pvPtr->end) {
  272.         /*
  273.          * Target buffer space is about to run out.  Make more space.
  274.          */
  275.  
  276.         pvPtr->next = dst;
  277.         (*pvPtr->expandProc)(pvPtr, 1);
  278.         dst = pvPtr->next;
  279.     }
  280.  
  281.     c = *src;
  282.     src++;
  283.     if (c == termChar) {
  284.         *dst = '\0';
  285.         pvPtr->next = dst;
  286.         *termPtr = src;
  287.         return TCL_OK;
  288.     } else if (CHAR_TYPE(c) == TCL_NORMAL) {
  289.         copy:
  290.         *dst = c;
  291.         dst++;
  292.         continue;
  293.     } else if (c == '$') {
  294.         int length;
  295.         char *value;
  296.  
  297.         value = Tcl_ParseVar(interp, src-1, termPtr);
  298.         if (value == NULL) {
  299.         return TCL_ERROR;
  300.         }
  301.         src = *termPtr;
  302.         length = strlen(value);
  303.         if ((pvPtr->end - dst) <= length) {
  304.         pvPtr->next = dst;
  305.         (*pvPtr->expandProc)(pvPtr, length);
  306.         dst = pvPtr->next;
  307.         }
  308.         strcpy(dst, value);
  309.         dst += length;
  310.         continue;
  311.     } else if (c == '[') {
  312.         int result;
  313.  
  314.         pvPtr->next = dst;
  315.         result = TclParseNestedCmd(interp, src, flags, termPtr, pvPtr);
  316.         if (result != TCL_OK) {
  317.         return result;
  318.         }
  319.         src = *termPtr;
  320.         dst = pvPtr->next;
  321.         continue;
  322.     } else if (c == '\\') {
  323.         int numRead;
  324.  
  325.         src--;
  326.         *dst = Tcl_Backslash(src, &numRead);
  327.         dst++;
  328.         src += numRead;
  329.         continue;
  330.     } else if (c == '\0') {
  331.         Tcl_ResetResult(interp);
  332.         sprintf(interp->result, "missing %c", termChar);
  333.         *termPtr = string-1;
  334.         return TCL_ERROR;
  335.     } else {
  336.         goto copy;
  337.     }
  338.     }
  339. }
  340.  
  341. /*
  342.  *--------------------------------------------------------------
  343.  *
  344.  * TclParseNestedCmd --
  345.  *
  346.  *    This procedure parses a nested Tcl command between
  347.  *    brackets, returning the result of the command.
  348.  *
  349.  * Results:
  350.  *    The return value is a standard Tcl result, which is
  351.  *    TCL_OK unless there was an error while executing the
  352.  *    nested command.  If an error occurs then interp->result
  353.  *    contains a standard error message.  *TermPtr is filled
  354.  *    in with the address of the character just after the
  355.  *    last one processed;  this is usually the character just
  356.  *    after the matching close-bracket, or the null character
  357.  *    at the end of the string if the close-bracket was missing
  358.  *    (a missing close bracket is an error).  The result returned
  359.  *    by the command is stored in standard fashion in *pvPtr,
  360.  *    null-terminated, with pvPtr->next pointing to the null
  361.  *    character.
  362.  *
  363.  * Side effects:
  364.  *    The storage space at *pvPtr may be expanded.
  365.  *
  366.  *--------------------------------------------------------------
  367.  */
  368.  
  369. int
  370. TclParseNestedCmd(interp, string, flags, termPtr, pvPtr)
  371.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  372.                  * evaluations and error messages. */
  373.     char *string;        /* Character just after opening bracket. */
  374.     int flags;            /* Flags to pass to nested Tcl_Eval. */
  375.     char **termPtr;        /* Store address of terminating character
  376.                  * here. */
  377.     register ParseValue *pvPtr;    /* Information about where to place
  378.                  * result of command. */
  379. {
  380.     int result, length, shortfall;
  381.     Interp *iPtr = (Interp *) interp;
  382.  
  383.     iPtr->evalFlags = flags | TCL_BRACKET_TERM;
  384.     result = Tcl_Eval(interp, string);
  385.     *termPtr = iPtr->termPtr;
  386.     if (result != TCL_OK) {
  387.     /*
  388.      * The increment below results in slightly cleaner message in
  389.      * the errorInfo variable (the close-bracket will appear).
  390.      */
  391.  
  392.     if (**termPtr == ']') {
  393.         *termPtr += 1;
  394.     }
  395.     return result;
  396.     }
  397.     (*termPtr) += 1;
  398.     length = strlen(iPtr->result);
  399.     shortfall = length + 1 - (pvPtr->end - pvPtr->next);
  400.     if (shortfall > 0) {
  401.     (*pvPtr->expandProc)(pvPtr, shortfall);
  402.     }
  403.     strcpy(pvPtr->next, iPtr->result);
  404.     pvPtr->next += length;
  405.     Tcl_FreeResult(iPtr);
  406.     iPtr->result = iPtr->resultSpace;
  407.     iPtr->resultSpace[0] = '\0';
  408.     return TCL_OK;
  409. }
  410.  
  411. /*
  412.  *--------------------------------------------------------------
  413.  *
  414.  * TclParseBraces --
  415.  *
  416.  *    This procedure scans the information between matching
  417.  *    curly braces.
  418.  *
  419.  * Results:
  420.  *    The return value is a standard Tcl result, which is
  421.  *    TCL_OK unless there was an error while parsing string.
  422.  *    If an error occurs then interp->result contains a
  423.  *    standard error message.  *TermPtr is filled
  424.  *    in with the address of the character just after the
  425.  *    last one successfully processed;  this is usually the
  426.  *    character just after the matching close-brace.  The
  427.  *    information between curly braces is stored in standard
  428.  *    fashion in *pvPtr, null-terminated with pvPtr->next
  429.  *    pointing to the terminating null character.
  430.  *
  431.  * Side effects:
  432.  *    The storage space at *pvPtr may be expanded.
  433.  *
  434.  *--------------------------------------------------------------
  435.  */
  436.  
  437. int
  438. TclParseBraces(interp, string, termPtr, pvPtr)
  439.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  440.                  * evaluations and error messages. */
  441.     char *string;        /* Character just after opening bracket. */
  442.     char **termPtr;        /* Store address of terminating character
  443.                  * here. */
  444.     register ParseValue *pvPtr;    /* Information about where to place
  445.                  * result of command. */
  446. {
  447.     int level;
  448.     register char *src, *dst, *end;
  449.     register char c;
  450.  
  451.     src = string;
  452.     dst = pvPtr->next;
  453.     end = pvPtr->end;
  454.     level = 1;
  455.  
  456.     /*
  457.      * Copy the characters one at a time to the result area, stopping
  458.      * when the matching close-brace is found.
  459.      */
  460.  
  461.     while (1) {
  462.     c = *src;
  463.     src++;
  464.     if (dst == end) {
  465.         pvPtr->next = dst;
  466.         (*pvPtr->expandProc)(pvPtr, 20);
  467.         dst = pvPtr->next;
  468.         end = pvPtr->end;
  469.     }
  470.     *dst = c;
  471.     dst++;
  472.     if (CHAR_TYPE(c) == TCL_NORMAL) {
  473.         continue;
  474.     } else if (c == '{') {
  475.         level++;
  476.     } else if (c == '}') {
  477.         level--;
  478.         if (level == 0) {
  479.         dst--;            /* Don't copy the last close brace. */
  480.         break;
  481.         }
  482.     } else if (c == '\\') {
  483.         int count;
  484.  
  485.         /*
  486.          * Must always squish out backslash-newlines, even when in
  487.          * braces.  This is needed so that this sequence can appear
  488.          * anywhere in a command, such as the middle of an expression.
  489.          */
  490.  
  491.         if (*src == '\n') {
  492.         dst[-1] = Tcl_Backslash(src-1, &count);
  493.         src += count - 1;
  494.         } else {
  495.         (void) Tcl_Backslash(src-1, &count);
  496.         while (count > 1) {
  497.                     if (dst == end) {
  498.                         pvPtr->next = dst;
  499.                         (*pvPtr->expandProc)(pvPtr, 20);
  500.                         dst = pvPtr->next;
  501.                         end = pvPtr->end;
  502.                     }
  503.             *dst = *src;
  504.             dst++;
  505.             src++;
  506.             count--;
  507.         }
  508.         }
  509.     } else if (c == '\0') {
  510.         Tcl_SetResult(interp, "missing close-brace", TCL_STATIC);
  511.         *termPtr = string-1;
  512.         return TCL_ERROR;
  513.     }
  514.     }
  515.  
  516.     *dst = '\0';
  517.     pvPtr->next = dst;
  518.     *termPtr = src;
  519.     return TCL_OK;
  520. }
  521.  
  522. /*
  523.  *--------------------------------------------------------------
  524.  *
  525.  * TclParseWords --
  526.  *
  527.  *    This procedure parses one or more words from a command
  528.  *    string and creates argv-style pointers to fully-substituted
  529.  *    copies of those words.
  530.  *
  531.  * Results:
  532.  *    The return value is a standard Tcl result.
  533.  *    
  534.  *    *argcPtr is modified to hold a count of the number of words
  535.  *    successfully parsed, which may be 0.  At most maxWords words
  536.  *    will be parsed.  If 0 <= *argcPtr < maxWords then it
  537.  *    means that a command separator was seen.  If *argcPtr
  538.  *    is maxWords then it means that a command separator was
  539.  *    not seen yet.
  540.  *
  541.  *    *TermPtr is filled in with the address of the character
  542.  *    just after the last one successfully processed in the
  543.  *    last word.  This is either the command terminator (if
  544.  *    *argcPtr < maxWords), the character just after the last
  545.  *    one in a word (if *argcPtr is maxWords), or the vicinity
  546.  *    of an error (if the result is not TCL_OK).
  547.  *    
  548.  *    The pointers at *argv are filled in with pointers to the
  549.  *    fully-substituted words, and the actual contents of the
  550.  *    words are copied to the buffer at pvPtr.
  551.  *
  552.  *    If an error occurrs then an error message is left in
  553.  *    interp->result and the information at *argv, *argcPtr,
  554.  *    and *pvPtr may be incomplete.
  555.  *
  556.  * Side effects:
  557.  *    The buffer space in pvPtr may be enlarged by calling its
  558.  *    expandProc.
  559.  *
  560.  *--------------------------------------------------------------
  561.  */
  562.  
  563. int
  564. TclParseWords(interp, string, flags, maxWords, termPtr, argcPtr, argv, pvPtr)
  565.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  566.                  * evaluations and error messages. */
  567.     char *string;        /* First character of word. */
  568.     int flags;            /* Flags to control parsing (same values as
  569.                  * passed to Tcl_Eval). */
  570.     int maxWords;        /* Maximum number of words to parse. */
  571.     char **termPtr;        /* Store address of terminating character
  572.                  * here. */
  573.     int *argcPtr;        /* Filled in with actual number of words
  574.                  * parsed. */
  575.     char **argv;        /* Store addresses of individual words here. */
  576.     register ParseValue *pvPtr;    /* Information about where to place
  577.                  * fully-substituted word. */
  578. {
  579.     register char *src, *dst;
  580.     register char c;
  581.     int type, result, argc;
  582.     char *oldBuffer;        /* Used to detect when pvPtr's buffer gets
  583.                  * reallocated, so we can adjust all of the
  584.                  * argv pointers. */
  585.  
  586.     src = string;
  587.     oldBuffer = pvPtr->buffer;
  588.     dst = pvPtr->next;
  589.     for (argc = 0; argc < maxWords; argc++) {
  590.     argv[argc] = dst;
  591.  
  592.     /*
  593.      * Skip leading space.
  594.      */
  595.     
  596.     skipSpace:
  597.     c = *src;
  598.     type = CHAR_TYPE(c);
  599.     while (type == TCL_SPACE) {
  600.         src++;
  601.         c = *src;
  602.         type = CHAR_TYPE(c);
  603.     }
  604.     
  605.     /*
  606.      * Handle the normal case (i.e. no leading double-quote or brace).
  607.      */
  608.  
  609.     if (type == TCL_NORMAL) {
  610.         normalArg:
  611.         while (1) {
  612.         if (dst == pvPtr->end) {
  613.             /*
  614.              * Target buffer space is about to run out.  Make
  615.              * more space.
  616.              */
  617.     
  618.             pvPtr->next = dst;
  619.             (*pvPtr->expandProc)(pvPtr, 1);
  620.             dst = pvPtr->next;
  621.         }
  622.     
  623.         if (type == TCL_NORMAL) {
  624.             copy:
  625.             *dst = c;
  626.             dst++;
  627.             src++;
  628.         } else if (type == TCL_SPACE) {
  629.             goto wordEnd;
  630.         } else if (type == TCL_DOLLAR) {
  631.             int length;
  632.             char *value;
  633.     
  634.             value = Tcl_ParseVar(interp, src, termPtr);
  635.             if (value == NULL) {
  636.             return TCL_ERROR;
  637.             }
  638.             src = *termPtr;
  639.             length = strlen(value);
  640.             if ((pvPtr->end - dst) <= length) {
  641.             pvPtr->next = dst;
  642.             (*pvPtr->expandProc)(pvPtr, length);
  643.             dst = pvPtr->next;
  644.             }
  645.             strcpy(dst, value);
  646.             dst += length;
  647.         } else if (type == TCL_COMMAND_END) {
  648.             if ((c == ']') && !(flags & TCL_BRACKET_TERM)) {
  649.             goto copy;
  650.             }
  651.  
  652.             /*
  653.              * End of command;  simulate a word-end first, so
  654.              * that the end-of-command can be processed as the
  655.              * first thing in a new word.
  656.              */
  657.  
  658.             goto wordEnd;
  659.         } else if (type == TCL_OPEN_BRACKET) {
  660.             pvPtr->next = dst;
  661.             result = TclParseNestedCmd(interp, src+1, flags, termPtr,
  662.                 pvPtr);
  663.             if (result != TCL_OK) {
  664.             return result;
  665.             }
  666.             src = *termPtr;
  667.             dst = pvPtr->next;
  668.         } else if (type == TCL_BACKSLASH) {
  669.             int numRead;
  670.     
  671.             *dst = Tcl_Backslash(src, &numRead);
  672.             dst++;
  673.             src += numRead;
  674.         } else {
  675.             goto copy;
  676.         }
  677.         c = *src;
  678.         type = CHAR_TYPE(c);
  679.         }
  680.     } else {
  681.     
  682.         /*
  683.          * Check for the end of the command.
  684.          */
  685.     
  686.         if (type == TCL_COMMAND_END) {
  687.         if (flags & TCL_BRACKET_TERM) {
  688.             if (c == '\0') {
  689.             Tcl_SetResult(interp, "missing close-bracket",
  690.                 TCL_STATIC);
  691.             return TCL_ERROR;
  692.             }
  693.         } else {
  694.             if (c == ']') {
  695.             goto normalArg;
  696.             }
  697.         }
  698.         goto done;
  699.         }
  700.     
  701.         /*
  702.          * Now handle the special cases: open braces, double-quotes,
  703.          * and backslash-newline.
  704.          */
  705.  
  706.         pvPtr->next = dst;
  707.         if (type == TCL_QUOTE) {
  708.         result = TclParseQuotes(interp, src+1, '"', flags,
  709.             termPtr, pvPtr);
  710.         } else if (type == TCL_OPEN_BRACE) {
  711.         result = TclParseBraces(interp, src+1, termPtr, pvPtr);
  712.         } else if ((type == TCL_BACKSLASH) && (src[1] == '\n')) {
  713.         /*
  714.          * This code is needed so that a backslash-newline at the
  715.          * very beginning of a word is treated as part of the white
  716.          * space between words and not as a space within the word.
  717.          */
  718.  
  719.         src += 2;
  720.         goto skipSpace;
  721.         } else {
  722.         goto normalArg;
  723.         }
  724.         if (result != TCL_OK) {
  725.         return result;
  726.         }
  727.     
  728.         /*
  729.          * Back from quotes or braces;  make sure that the terminating
  730.          * character was the end of the word.  Have to be careful here
  731.          * to handle continuation lines (i.e. lines ending in backslash).
  732.          */
  733.     
  734.         c = **termPtr;
  735.         if ((c == '\\') && ((*termPtr)[1] == '\n')) {
  736.         c = (*termPtr)[2];
  737.         }
  738.         type = CHAR_TYPE(c);
  739.         if ((type != TCL_SPACE) && (type != TCL_COMMAND_END)) {
  740.         if (*src == '"') {
  741.             Tcl_SetResult(interp, "extra characters after close-quote",
  742.                 TCL_STATIC);
  743.         } else {
  744.             Tcl_SetResult(interp, "extra characters after close-brace",
  745.                 TCL_STATIC);
  746.         }
  747.         return TCL_ERROR;
  748.         }
  749.         src = *termPtr;
  750.         dst = pvPtr->next;
  751.  
  752.     }
  753.  
  754.     /*
  755.      * We're at the end of a word, so add a null terminator.  Then
  756.      * see if the buffer was re-allocated during this word.  If so,
  757.      * update all of the argv pointers.
  758.      */
  759.  
  760.     wordEnd:
  761.     *dst = '\0';
  762.     dst++;
  763.     if (oldBuffer != pvPtr->buffer) {
  764.         int i;
  765.  
  766.         for (i = 0; i <= argc; i++) {
  767.         argv[i] = pvPtr->buffer + (argv[i] - oldBuffer);
  768.         }
  769.         oldBuffer = pvPtr->buffer;
  770.     }
  771.     }
  772.  
  773.     done:
  774.     pvPtr->next = dst;
  775.     *termPtr = src;
  776.     *argcPtr = argc;
  777.     return TCL_OK;
  778. }
  779.  
  780. /*
  781.  *--------------------------------------------------------------
  782.  *
  783.  * TclExpandParseValue --
  784.  *
  785.  *    This procedure is commonly used as the value of the
  786.  *    expandProc in a ParseValue.  It uses malloc to allocate
  787.  *    more space for the result of a parse.
  788.  *
  789.  * Results:
  790.  *    The buffer space in *pvPtr is reallocated to something
  791.  *    larger, and if pvPtr->clientData is non-zero the old
  792.  *    buffer is freed.  Information is copied from the old
  793.  *    buffer to the new one.
  794.  *
  795.  * Side effects:
  796.  *    None.
  797.  *
  798.  *--------------------------------------------------------------
  799.  */
  800.  
  801. void
  802. TclExpandParseValue(pvPtr, needed)
  803.     register ParseValue *pvPtr;        /* Information about buffer that
  804.                      * must be expanded.  If the clientData
  805.                      * in the structure is non-zero, it
  806.                      * means that the current buffer is
  807.                      * dynamically allocated. */
  808.     int needed;                /* Minimum amount of additional space
  809.                      * to allocate. */
  810. {
  811.     int newSpace;
  812.     char *new;
  813.  
  814.     /*
  815.      * Either double the size of the buffer or add enough new space
  816.      * to meet the demand, whichever produces a larger new buffer.
  817.      */
  818.  
  819.     newSpace = (pvPtr->end - pvPtr->buffer) + 1;
  820.     if (newSpace < needed) {
  821.     newSpace += needed;
  822.     } else {
  823.     newSpace += newSpace;
  824.     }
  825.     new = (char *) ckalloc((unsigned) newSpace);
  826.  
  827.     /*
  828.      * Copy from old buffer to new, free old buffer if needed, and
  829.      * mark new buffer as malloc-ed.
  830.      */
  831.  
  832.     memcpy((VOID *) new, (VOID *) pvPtr->buffer, pvPtr->next - pvPtr->buffer);
  833.     pvPtr->next = new + (pvPtr->next - pvPtr->buffer);
  834.     if (pvPtr->clientData != 0) {
  835.     ckfree(pvPtr->buffer);
  836.     }
  837.     pvPtr->buffer = new;
  838.     pvPtr->end = new + newSpace - 1;
  839.     pvPtr->clientData = (ClientData) 1;
  840. }
  841.  
  842. /*
  843.  *----------------------------------------------------------------------
  844.  *
  845.  * TclWordEnd --
  846.  *
  847.  *    Given a pointer into a Tcl command, find the end of the next
  848.  *    word of the command.
  849.  *
  850.  * Results:
  851.  *    The return value is a pointer to the last character that's part
  852.  *    of the word pointed to by "start".  If the word doesn't end
  853.  *    properly within the string then the return value is the address
  854.  *    of the null character at the end of the string.
  855.  *
  856.  * Side effects:
  857.  *    None.
  858.  *
  859.  *----------------------------------------------------------------------
  860.  */
  861.  
  862. char *
  863. TclWordEnd(start, nested)
  864.     char *start;        /* Beginning of a word of a Tcl command. */
  865.     int nested;            /* Zero means this is a top-level command.
  866.                  * One means this is a nested command (close
  867.                  * brace is a word terminator). */
  868. {
  869.     register char *p;
  870.     int count;
  871.  
  872.     /*
  873.      * Skip leading white space (backslash-newline must be treated like
  874.      * white-space, except that it better not be the last thing in the
  875.      * command).
  876.      */
  877.  
  878.     for (p = start; ; p++) {
  879.     if (isspace(*p)) {
  880.         continue;
  881.     }
  882.     if ((p[0] == '\\') && (p[1] == '\n')) {
  883.         if (p[2] == 0) {
  884.         return p+2;
  885.         }
  886.         continue;
  887.     }
  888.     break;
  889.     }
  890.  
  891.     /*
  892.      * Handle words beginning with a double-quote or a brace.
  893.      */
  894.  
  895.     if (*p == '"') {
  896.     p = QuoteEnd(p+1, '"');
  897.     if (*p == 0) {
  898.         return p;
  899.     }
  900.     p++;
  901.     } else if (*p == '{') {
  902.     int braces = 1;
  903.     while (braces != 0) {
  904.         p++;
  905.         while (*p == '\\') {
  906.         (void) Tcl_Backslash(p, &count);
  907.         p += count;
  908.         }
  909.         if (*p == '}') {
  910.         braces--;
  911.         } else if (*p == '{') {
  912.         braces++;
  913.         } else if (*p == 0) {
  914.         return p;
  915.         }
  916.     }
  917.     p++;
  918.     }
  919.  
  920.     /*
  921.      * Handle words that don't start with a brace or double-quote.
  922.      * This code is also invoked if the word starts with a brace or
  923.      * double-quote and there is garbage after the closing brace or
  924.      * quote.  This is an error as far as Tcl_Eval is concerned, but
  925.      * for here the garbage is treated as part of the word.
  926.      */
  927.  
  928.     while (1) {
  929.     if (*p == '[') {
  930.         for (p++; *p != ']'; p++) {
  931.         p = TclWordEnd(p, 1);
  932.         if (*p == 0) {
  933.             return p;
  934.         }
  935.         }
  936.         p++;
  937.     } else if (*p == '\\') {
  938.         (void) Tcl_Backslash(p, &count);
  939.         p += count;
  940.         if ((*p == 0) && (count == 2) && (p[-1] == '\n')) {
  941.         return p;
  942.         }
  943.     } else if (*p == '$') {
  944.         p = VarNameEnd(p);
  945.         if (*p == 0) {
  946.         return p;
  947.         }
  948.         p++;
  949.     } else if (*p == ';') {
  950.         /*
  951.          * Include the semi-colon in the word that is returned.
  952.          */
  953.  
  954.         return p;
  955.     } else if (isspace(*p)) {
  956.         return p-1;
  957.     } else if ((*p == ']') && nested) {
  958.         return p-1;
  959.     } else if (*p == 0) {
  960.         if (nested) {
  961.         /*
  962.          * Nested commands can't end because of the end of the
  963.          * string.
  964.          */
  965.         return p;
  966.         }
  967.         return p-1;
  968.     } else {
  969.         p++;
  970.     }
  971.     }
  972. }
  973.  
  974. /*
  975.  *----------------------------------------------------------------------
  976.  *
  977.  * QuoteEnd --
  978.  *
  979.  *    Given a pointer to a string that obeys the parsing conventions
  980.  *    for quoted things in Tcl, find the end of that quoted thing.
  981.  *    The actual thing may be a quoted argument or a parenthesized
  982.  *    index name.
  983.  *
  984.  * Results:
  985.  *    The return value is a pointer to the last character that is
  986.  *    part of the quoted string (i.e the character that's equal to
  987.  *    term).  If the quoted string doesn't terminate properly then
  988.  *    the return value is a pointer to the null character at the
  989.  *    end of the string.
  990.  *
  991.  * Side effects:
  992.  *    None.
  993.  *
  994.  *----------------------------------------------------------------------
  995.  */
  996.  
  997. static char *
  998. QuoteEnd(string, term)
  999.     char *string;        /* Pointer to character just after opening
  1000.                  * "quote". */
  1001.     int term;            /* This character will terminate the
  1002.                  * quoted string (e.g. '"' or ')'). */
  1003. {
  1004.     register char *p = string;
  1005.     int count;
  1006.  
  1007.     while (*p != term) {
  1008.     if (*p == '\\') {
  1009.         (void) Tcl_Backslash(p, &count);
  1010.         p += count;
  1011.     } else if (*p == '[') {
  1012.         for (p++; *p != ']'; p++) {
  1013.         p = TclWordEnd(p, 1);
  1014.         if (*p == 0) {
  1015.             return p;
  1016.         }
  1017.         }
  1018.         p++;
  1019.     } else if (*p == '$') {
  1020.         p = VarNameEnd(p);
  1021.         if (*p == 0) {
  1022.         return p;
  1023.         }
  1024.         p++;
  1025.     } else if (*p == 0) {
  1026.         return p;
  1027.     } else {
  1028.         p++;
  1029.     }
  1030.     }
  1031.     return p-1;
  1032. }
  1033.  
  1034. /*
  1035.  *----------------------------------------------------------------------
  1036.  *
  1037.  * VarNameEnd --
  1038.  *
  1039.  *    Given a pointer to a variable reference using $-notation, find
  1040.  *    the end of the variable name spec.
  1041.  *
  1042.  * Results:
  1043.  *    The return value is a pointer to the last character that
  1044.  *    is part of the variable name.  If the variable name doesn't
  1045.  *    terminate properly then the return value is a pointer to the
  1046.  *    null character at the end of the string.
  1047.  *
  1048.  * Side effects:
  1049.  *    None.
  1050.  *
  1051.  *----------------------------------------------------------------------
  1052.  */
  1053.  
  1054. static char *
  1055. VarNameEnd(string)
  1056.     char *string;        /* Pointer to dollar-sign character. */
  1057. {
  1058.     register char *p = string+1;
  1059.  
  1060.     if (*p == '{') {
  1061.     for (p++; (*p != '}') && (*p != 0); p++) {
  1062.         /* Empty loop body. */
  1063.     }
  1064.     return p;
  1065.     }
  1066.     while (isalnum(*p) || (*p == '_')) {
  1067.     p++;
  1068.     }
  1069.     if ((*p == '(') && (p != string+1)) {
  1070.     return QuoteEnd(p+1, ')');
  1071.     }
  1072.     return p-1;
  1073. }
  1074.  
  1075. /*
  1076.  *----------------------------------------------------------------------
  1077.  *
  1078.  * Tcl_ParseVar --
  1079.  *
  1080.  *    Given a string starting with a $ sign, parse off a variable
  1081.  *    name and return its value.
  1082.  *
  1083.  * Results:
  1084.  *    The return value is the contents of the variable given by
  1085.  *    the leading characters of string.  If termPtr isn't NULL,
  1086.  *    *termPtr gets filled in with the address of the character
  1087.  *    just after the last one in the variable specifier.  If the
  1088.  *    variable doesn't exist, then the return value is NULL and
  1089.  *    an error message will be left in interp->result.
  1090.  *
  1091.  * Side effects:
  1092.  *    None.
  1093.  *
  1094.  *----------------------------------------------------------------------
  1095.  */
  1096.  
  1097. char *
  1098. Tcl_ParseVar(interp, string, termPtr)
  1099.     Tcl_Interp *interp;            /* Context for looking up variable. */
  1100.     register char *string;        /* String containing variable name.
  1101.                      * First character must be "$". */
  1102.     char **termPtr;            /* If non-NULL, points to word to fill
  1103.                      * in with character just after last
  1104.                      * one in the variable specifier. */
  1105.  
  1106. {
  1107.     char *name1, *name1End, c, *result;
  1108.     register char *name2;
  1109. #define NUM_CHARS 200
  1110.     char copyStorage[NUM_CHARS];
  1111.     ParseValue pv;
  1112.  
  1113.     /*
  1114.      * There are three cases:
  1115.      * 1. The $ sign is followed by an open curly brace.  Then the variable
  1116.      *    name is everything up to the next close curly brace, and the
  1117.      *    variable is a scalar variable.
  1118.      * 2. The $ sign is not followed by an open curly brace.  Then the
  1119.      *    variable name is everything up to the next character that isn't
  1120.      *    a letter, digit, or underscore.  If the following character is an
  1121.      *    open parenthesis, then the information between parentheses is
  1122.      *    the array element name, which can include any of the substitutions
  1123.      *    permissible between quotes.
  1124.      * 3. The $ sign is followed by something that isn't a letter, digit,
  1125.      *    or underscore:  in this case, there is no variable name, and "$"
  1126.      *    is returned.
  1127.      */
  1128.  
  1129.     name2 = NULL;
  1130.     string++;
  1131.     if (*string == '{') {
  1132.     string++;
  1133.     name1 = string;
  1134.     while (*string != '}') {
  1135.         if (*string == 0) {
  1136.         Tcl_SetResult(interp, "missing close-brace for variable name",
  1137.             TCL_STATIC);
  1138.         if (termPtr != 0) {
  1139.             *termPtr = string;
  1140.         }
  1141.         return NULL;
  1142.         }
  1143.         string++;
  1144.     }
  1145.     name1End = string;
  1146.     string++;
  1147.     } else {
  1148.     name1 = string;
  1149.     while (isalnum(*string) || (*string == '_')) {
  1150.         string++;
  1151.     }
  1152.     if (string == name1) {
  1153.         if (termPtr != 0) {
  1154.         *termPtr = string;
  1155.         }
  1156.         return "$";
  1157.     }
  1158.     name1End = string;
  1159.     if (*string == '(') {
  1160.         char *end;
  1161.  
  1162.         /*
  1163.          * Perform substitutions on the array element name, just as
  1164.          * is done for quotes.
  1165.          */
  1166.  
  1167.         pv.buffer = pv.next = copyStorage;
  1168.         pv.end = copyStorage + NUM_CHARS - 1;
  1169.         pv.expandProc = TclExpandParseValue;
  1170.         pv.clientData = (ClientData) NULL;
  1171.         if (TclParseQuotes(interp, string+1, ')', 0, &end, &pv)
  1172.             != TCL_OK) {
  1173.         char msg[100];
  1174.         sprintf(msg, "\n    (parsing index for array \"%.*s\")",
  1175.             string-name1, name1);
  1176.         Tcl_AddErrorInfo(interp, msg);
  1177.         result = NULL;
  1178.         name2 = pv.buffer;
  1179.         if (termPtr != 0) {
  1180.             *termPtr = end;
  1181.         }
  1182.         goto done;
  1183.         }
  1184.         Tcl_ResetResult(interp);
  1185.         string = end;
  1186.         name2 = pv.buffer;
  1187.     }
  1188.     }
  1189.     if (termPtr != 0) {
  1190.     *termPtr = string;
  1191.     }
  1192.  
  1193.     if (((Interp *) interp)->noEval) {
  1194.     return "";
  1195.     }
  1196.     c = *name1End;
  1197.     *name1End = 0;
  1198.     result = Tcl_GetVar2(interp, name1, name2, TCL_LEAVE_ERR_MSG);
  1199.     *name1End = c;
  1200.  
  1201.     done:
  1202.     if ((name2 != NULL) && (pv.buffer != copyStorage)) {
  1203.     ckfree(pv.buffer);
  1204.     }
  1205.     return result;
  1206. }
  1207.  
  1208. /*
  1209.  *----------------------------------------------------------------------
  1210.  *
  1211.  * Tcl_CommandComplete --
  1212.  *
  1213.  *    Given a partial or complete Tcl command, this procedure
  1214.  *    determines whether the command is complete in the sense
  1215.  *    of having matched braces and quotes and brackets.
  1216.  *
  1217.  * Results:
  1218.  *    1 is returned if the command is complete, 0 otherwise.
  1219.  *
  1220.  * Side effects:
  1221.  *    None.
  1222.  *
  1223.  *----------------------------------------------------------------------
  1224.  */
  1225.  
  1226. int
  1227. Tcl_CommandComplete(cmd)
  1228.     char *cmd;            /* Command to check. */
  1229. {
  1230.     register char *p = cmd;
  1231.  
  1232.     p = cmd;
  1233.     while (1) {
  1234.     while (isspace(*p)) {
  1235.         p++;
  1236.     }
  1237.     if (*p == 0) {
  1238.         return 1;
  1239.     }
  1240.     p = TclWordEnd(p, 0);
  1241.     if (*p == 0) {
  1242.         return 0;
  1243.     }
  1244.     p++;
  1245.     }
  1246. }
  1247.